package com.example.springbootpay.service;

import com.example.springbootpay.service.impl.AlipayPayment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class PaymentStrategyFactory {
    private final Map<String, AbstractPaymentTemplate> strategyMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void initStrategies() {
        // 注册支付宝支付策略
        registerStrategy("alipay", new AlipayPayment());
        // 注册微信支付策略
        registerStrategy("wechatpay", new WechatPayment());
        // 注册闪付策略
        registerStrategy("flashpay", new FlashPayment());
        // 后续可扩展其他支付渠道
    }

    public void registerStrategy(String channel, AbstractPaymentTemplate strategy) {
        strategyMap.put(channel, strategy);
    }

    public AbstractPaymentTemplate getStrategy(String channel) {
        AbstractPaymentTemplate strategy = strategyMap.get(channel);
        if (strategy == null) {
            throw new IllegalArgumentException("Unsupported payment channel: " + channel);
        }
        return strategy;
    }
}